
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>Spring事件 | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="1. 概述在本文中，我们将讨论如何在Spring中使用事件。 事件是框架中被忽视的功能之一，但也是非常有用的功能之一，并且像Spring中的许多其他能力一样，事件发布是">
<meta name="keywords" content="Spring5">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring事件">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2018/12/08/Spring事件/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="1. 概述在本文中，我们将讨论如何在Spring中使用事件。 事件是框架中被忽视的功能之一，但也是非常有用的功能之一，并且像Spring中的许多其他能力一样，事件发布是">
<meta property="og:updated_time" content="2018-12-08T10:21:50.695Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring事件">
<meta name="twitter:description" content="1. 概述在本文中，我们将讨论如何在Spring中使用事件。 事件是框架中被忽视的功能之一，但也是非常有用的功能之一，并且像Spring中的许多其他能力一样，事件发布是">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2018/12/08/Spring事件/" title="Spring事件" itemprop="url">Spring事件</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2018-12-08T10:00:59.000Z" itemprop="datePublished"> Published 2018-12-08</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-概述"><span class="toc-number">1.</span> <span class="toc-text">1. 概述</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-自定义事件"><span class="toc-number">2.</span> <span class="toc-text">2. 自定义事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-创建异步事件"><span class="toc-number">3.</span> <span class="toc-text">3. 创建异步事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-框架中存在的事件"><span class="toc-number">4.</span> <span class="toc-text">4. 框架中存在的事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-注解驱动的事件监听器"><span class="toc-number">5.</span> <span class="toc-text">5. 注解驱动的事件监听器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-泛型支持"><span class="toc-number">6.</span> <span class="toc-text">6. 泛型支持</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-事务绑定事件"><span class="toc-number">7.</span> <span class="toc-text">7. 事务绑定事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">8.</span> <span class="toc-text">总结</span></a></li></ol>
		
		</div>
		
		<h5 id="1-概述"><a href="#1-概述" class="headerlink" title="1. 概述"></a>1. 概述</h5><pre><code>在本文中，我们将讨论如何在Spring中使用事件。
事件是框架中被忽视的功能之一，但也是非常有用的功能之一，并且像Spring中的许多其他能力一样，事件发布是
</code></pre><a id="more"></a>
<pre><code>ApplicationContext上下文提供的功能之一。
    有一些简单的指导方案可供参考：
</code></pre><ul>
<li>事件应该继承(扩展)ApplicationEvent</li>
<li>发布者应该注入一个ApplicationEventPublisher对象</li>
<li>监听器应该实现ApplicationListener接口</li>
</ul>
<h5 id="2-自定义事件"><a href="#2-自定义事件" class="headerlink" title="2. 自定义事件"></a>2. 自定义事件</h5><pre><code>Spring允许创建和发布自定义事件，默认情况下是同步的，这具有一些优点，例如监听器能够参与发布者的业务上下文。    
</code></pre><p><strong>2.1 简单的应用程序事件</strong></p>
<pre><code>创建一个简单的事件类，只是一个存储事件数据的占位符，在这种情况下，事件类包含String消息：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomSpringEvent</span> <span class="keyword">extends</span> <span class="title">ApplicationEvent</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> String message;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CustomSpringEvent</span><span class="params">(Object source, String message)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>(source);</div><div class="line">        <span class="keyword">this</span>.message = message;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getMessage</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> message;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>2.2 事件发布者</strong>    </p>
<pre><code>现在创建该事件的发布者，发布者构造事件对象并将其发布给正在收听的任何人。 
要发布事件，发布者只需注入ApplicationEventPublisher并使用publishEvent() API：  
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Component</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomSpringEventPublisher</span> </span>&#123;</div><div class="line">    <span class="meta">@Autowired</span></div><div class="line">    <span class="keyword">private</span> ApplicationEventPublisher applicationEventPublisher;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doStuffAndPublishAnEvent</span><span class="params">(<span class="keyword">final</span> String message)</span> </span>&#123;</div><div class="line">        System.out.println(<span class="string">"Publishing custom event. "</span>);</div><div class="line">        CustomSpringEvent customSpringEvent = <span class="keyword">new</span> CustomSpringEvent(<span class="keyword">this</span>, message);</div><div class="line">        applicationEventPublisher.publishEvent(customSpringEvent);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    或者，发布者类可以实现ApplicationEventPublisherAware接口,这也将在应用程序启动时注入事件
发布者,通常只需向@Autowire注入发布者就可以了。
</code></pre><p><strong>2.3 事件监听器</strong></p>
<pre><code>最后，创建一个事件监听器，监听器的唯一要求是成为bean并实现ApplicationListener接口：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Component</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomSpringEventListener</span> <span class="keyword">implements</span> <span class="title">ApplicationListener</span>&lt;<span class="title">CustomSpringEvent</span>&gt; </span>&#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onApplicationEvent</span><span class="params">(CustomSpringEvent event)</span> </span>&#123;</div><div class="line">        System.out.println(<span class="string">"Received spring custom event - "</span> + event.getMessage());</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    需要注意的是，我们的自定义监听器如何使用泛型事件的泛型类型进行参数化，这使得onApplicationEvent()
方法类型安全，也避免了必须检查对象是否是特定事件类的实例并将其强制转换。
    并且，如前所述，默认情况下，spring事件是同步的，doStuffAndPublishAnEvent()方法将阻塞，直到所有监听器
完成事件的处理。
</code></pre><h5 id="3-创建异步事件"><a href="#3-创建异步事件" class="headerlink" title="3. 创建异步事件"></a>3. 创建异步事件</h5><pre><code>    在某些情况下，同步发布事件并不是我们想要的，我们可能需要异步处理我们的事件。    
    你可以通过使用执行程序创建ApplicationEventMulticaster bean来在配置中启用它; 对于我们的特殊诉求，简单线程池
SimpleAsyncTaskExecutor能够运行良好：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AsynchronousSpringEventsConfig</span> </span>&#123;</div><div class="line">    <span class="meta">@Bean</span>(name = <span class="string">"applicationEventMulticaster"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> ApplicationEventMulticaster <span class="title">simpleApplicationEventMulticaster</span><span class="params">()</span> </span>&#123;</div><div class="line">        SimpleApplicationEventMulticaster eventMulticaster </div><div class="line">          = <span class="keyword">new</span> SimpleApplicationEventMulticaster();</div><div class="line">        eventMulticaster.setTaskExecutor(<span class="keyword">new</span> SimpleAsyncTaskExecutor());</div><div class="line">        <span class="keyword">return</span> eventMulticaster;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>事件，发布者和监听器实现与以前保持一致，但现在，监听器将在一个单独的线程中异步处理事件。
</code></pre><h5 id="4-框架中存在的事件"><a href="#4-框架中存在的事件" class="headerlink" title="4. 框架中存在的事件"></a>4. 框架中存在的事件</h5><pre><code>    Spring框架本身发布了很多开箱即用的事件，例如，ApplicationContext将触发各种框架事件：ContextRefreshedEvent，
ContextStartedEvent，RequestHandledEvent等。
   这些事件为应用程序开发人员提供了一个选择，提供一个钩子，将自己的自定义逻辑添加到应用程序和上下文的生命周期。     
   这是一个监听上下文刷新的监听器的示例：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ContextRefreshedListener</span> </span></div><div class="line">  <span class="keyword">implements</span> <span class="title">ApplicationListener</span>&lt;<span class="title">ContextRefreshedEvent</span>&gt; &#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onApplicationEvent</span><span class="params">(ContextRefreshedEvent cse)</span> </span>&#123;</div><div class="line">        System.out.println(<span class="string">"Handling context re-freshed event. "</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h5 id="5-注解驱动的事件监听器"><a href="#5-注解驱动的事件监听器" class="headerlink" title="5. 注解驱动的事件监听器"></a>5. 注解驱动的事件监听器</h5><pre><code>    从Spring 4.2开始，事件监听器不需要实现ApplicationListener接口
的bean，它可以通过@EventListener注解在托管bean的任何公共方法上注册：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Component</span></div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AnnotationDrivenContextStartedListener</span> </span>&#123;</div><div class="line">    <span class="comment">// @Async</span></div><div class="line">    <span class="meta">@EventListener</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleContextStart</span><span class="params">(ContextStartedEvent cse)</span> </span>&#123;</div><div class="line">        System.out.println(<span class="string">"Handling context started event."</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    和前边一样，方法签名声明它消费的事件类型，并且这个监听器是同步调用的，但现在添加@Async注解就可以
将其变成异步（不要忘记在应用程序中启用异步支持）。
</code></pre><h5 id="6-泛型支持"><a href="#6-泛型支持" class="headerlink" title="6. 泛型支持"></a>6. 泛型支持</h5><pre><code>也可以在事件类型中使用泛型信息派发事件。
</code></pre><p><strong>6.1 通用应用事件</strong></p>
<pre><code>创建一个通用事件类型，在我们的示例中，事件类包含任何内容和成功状态：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericSpringEvent</span>&lt;<span class="title">T</span>&gt; </span>&#123;</div><div class="line">    <span class="keyword">private</span> T what;</div><div class="line">    <span class="keyword">protected</span> <span class="keyword">boolean</span> success;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">GenericSpringEvent</span><span class="params">(T what, <span class="keyword">boolean</span> success)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.what = what;</div><div class="line">        <span class="keyword">this</span>.success = success;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">// ... standard getters</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>    注意GenericSpringEvent和CustomSpringEvent之间的区别，我们现在可以灵活地发布任何任意事件，并且不再需要
从ApplicationEvent扩展。
</code></pre><p><strong>6.2 监听器</strong></p>
<pre><code>现在创建一个该事件的监听器，我们可以通过像以前一样实现ApplicationListener接口来定义监听器：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Component</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericSpringEventListener</span> <span class="keyword">implements</span> <span class="title">ApplicationListener</span>&lt;<span class="title">GenericSpringEvent</span>&lt;<span class="title">String</span>&gt;&gt; </span>&#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onApplicationEvent</span><span class="params">(@NonNull GenericSpringEvent&lt;String&gt; event)</span> </span>&#123;</div><div class="line">        System.out.println(<span class="string">"Received spring generic event - "</span> + event.getWhat());</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>     但不幸的是，这个定义要求我们从ApplicationEvent类继承GenericSpringEvent，因此对于本篇文章，我们使用前面讨论过的
注解驱动的事件监听器。
     通过在@EventListener注解上定义布尔型SpEL表达式，也可以指定事件监听器。在这个案例中，只会为成功的String的
 GenericSpringEvent调用事件处理程序：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Component</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AnnotationDrivenEventListener</span> </span>&#123;</div><div class="line">    <span class="meta">@EventListener</span>(condition = <span class="string">"#event.success"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleSuccessful</span><span class="params">(GenericSpringEvent&lt;String&gt; event)</span> </span>&#123;</div><div class="line">        System.out.println(<span class="string">"Handling generic event (conditional)."</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><strong>6.3 发布事件</strong></p>
<pre><code>    事件发布者与上述类似。但是由于类型擦除，我们需要发布一个事件来解析我们要过滤的泛型参数。例如,GenericStringSpringEvent类
扩展了GenericSpringEvent&lt;String&gt;。      
    还有另一种发布事件的方式，如果我们从使用@EventListener注解的方法返回非null值作为结果，Spring框架将把该结果作为新事件发送
给我们。 此外，我们可以通过在事件处理结果中将它们返回到集合中来发布多个新事件。  
</code></pre><h5 id="7-事务绑定事件"><a href="#7-事务绑定事件" class="headerlink" title="7. 事务绑定事件"></a>7. 事务绑定事件</h5><pre><code>    本段内容是关于使用@TransactionalEventListener注解，要了解有关事务管理的更多信息，spring事务相关资料。
    从Spring 4.2开始，框架提供了一个新的@TransactionalEventListener注解，它是@EventListener的扩展，允许将事件的监听器绑定到
事务的一个阶段。
    绑定可以进行以下事务阶段：
</code></pre><ul>
<li>AFTER_COMMIT（默认值）-用于在事务成功完成时触发事件</li>
<li>AFTER_ROLLBACK  -如果事务已回滚会触发</li>
<li>AFTER_COMPLETION -如果事务已完成（AFTER_COMMIT和AFTER_ROLLBACK)会触发</li>
<li><p>BEFORE_COMMIT    -用于在事务提交之前触发事件</p>
<pre><code>这是事务事件监听器的简单示例：     
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@TransactionalEventListener</span>(phase = TransactionPhase.BEFORE_COMMIT)</div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleCustom</span><span class="params">(CustomSpringEvent event)</span> </span>&#123;</div><div class="line">    System.out.println(<span class="string">"Handling event inside a transaction BEFORE COMMIT."</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<pre><code>仅当存在事件生成器正在运行且即将提交的事务时，才会调用此监听器。
并且，如果没有正在运行的事务，则根本不发送事件，除非我们通过将fallbackExecution属性设置为true来覆盖它。
</code></pre><h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><pre><code>在这篇文章中，我们讨论了在Spring中处理事件的基础知识，创建一个简单的自定义事件，然后发布，最后在监听器中处理它。
我们还简要介绍了如何在配置中启用事件的异步处理。然后我们了解了Spring 4.2中引入的改进，例如注解驱动的监听器，
</code></pre><p>  更好的泛型支持以及绑定到事务阶段的事件。</p>
</li>
</ul>
  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/spring/">spring</a>
</div>


  <div class="article-tags">
  
  <span></span> <a href="/typhoon/tags/Spring5/">Spring5</a>
  </div>

</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2018/12/08/Spring事件/" data-title="Spring事件 | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/typhoon/2018/12/08/springboot之SpringBootServletInitializer/" title="springboot之SpringBootServletInitializer">
  <strong>上一篇：</strong><br/>
  <span>
  springboot之SpringBootServletInitializer</span>
</a>
</div>


<div class="next">
<a href="/typhoon/2018/12/08/Spring之BeanNameAware和BeanFactoryAware/"  title="Spring之BeanNameAware和BeanFactoryAware">
 <strong>下一篇：</strong><br/> 
 <span>Spring之BeanNameAware和BeanFactoryAware
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-概述"><span class="toc-number">1.</span> <span class="toc-text">1. 概述</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-自定义事件"><span class="toc-number">2.</span> <span class="toc-text">2. 自定义事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-创建异步事件"><span class="toc-number">3.</span> <span class="toc-text">3. 创建异步事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-框架中存在的事件"><span class="toc-number">4.</span> <span class="toc-text">4. 框架中存在的事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-注解驱动的事件监听器"><span class="toc-number">5.</span> <span class="toc-text">5. 注解驱动的事件监听器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-泛型支持"><span class="toc-number">6.</span> <span class="toc-text">6. 泛型支持</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-事务绑定事件"><span class="toc-number">7.</span> <span class="toc-text">7. 事务绑定事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">8.</span> <span class="toc-text">总结</span></a></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
